package cn.iocoder.yudao.module.game.controller.app.withdrawnRecord;

import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.game.controller.admin.withdrawnRecord.vo.WithdrawnRecordPageReqVO;
import cn.iocoder.yudao.module.game.controller.admin.withdrawnRecord.vo.WithdrawnRecordRespVO;
import cn.iocoder.yudao.module.game.controller.admin.withdrawnRecord.vo.WithdrawnRecordSaveReqVO;
import cn.iocoder.yudao.module.game.dal.dataobject.withdrawnRecord.WithdrawnRecordDO;
import cn.iocoder.yudao.module.game.service.withdrawnRecord.WithdrawnRecordService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.pay.enums.wallet.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 待提现")
@RestController
@RequestMapping("/game/withdrawn-record")
@Validated
public class AppWithdrawnRecordController {

    @Resource
    private WithdrawnRecordService withdrawnRecordService;

    @Resource
    private MemberUserService memberUserService;

    @Resource
    private PayWalletService payWalletService;

    @PostMapping("/create")
    @Operation(summary = "创建待提现")
    public CommonResult<Long> createWithdrawnRecord(@RequestBody WithdrawnRecordSaveReqVO createReqVO) {
        if(!memberUserService.checkPayPassword(createReqVO.getMemberId(), createReqVO.getPayPassword())) {
            return error(999, "Erro da senha");
        }

        PayWalletDO payWalletDO = payWalletService.getOrCreateWallet(createReqVO.getMemberId(), UserTypeEnum.MEMBER.getValue());

        payWalletService.reduceWalletBalance(payWalletDO.getId(),
                String.valueOf(System.currentTimeMillis()),
                PayWalletBizTypeEnum.WITHDRAWAL,
                createReqVO.getAmount().multiply(new BigDecimal(100000)).longValue());

        return success(withdrawnRecordService.createWithdrawnRecord(createReqVO));
    }

    @PostMapping("/check/pay/password")
    @Operation(summary = "创建待提现")
    public CommonResult<Long> checkPayPassword(@RequestBody WithdrawnRecordSaveReqVO createReqVO) {
        if(!memberUserService.checkPayPassword(createReqVO.getMemberId(), createReqVO.getPayPassword())) {
            return error(999, "Erro da senha");
        }

        return success(1L);
    }

    @PutMapping("/update")
    @Operation(summary = "更新待提现")
    public CommonResult<Boolean> updateWithdrawnRecord(@RequestBody WithdrawnRecordSaveReqVO updateReqVO) {
        withdrawnRecordService.updateWithdrawnRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除待提现")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteWithdrawnRecord(@RequestParam("id") Long id) {
        withdrawnRecordService.deleteWithdrawnRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得待提现")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<WithdrawnRecordRespVO> getWithdrawnRecord(@RequestParam("id") Long id) {
        WithdrawnRecordDO withdrawnRecord = withdrawnRecordService.getWithdrawnRecord(id);
        return success(BeanUtils.toBean(withdrawnRecord, WithdrawnRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得待提现分页")
    public CommonResult<PageResult<WithdrawnRecordRespVO>> getWithdrawnRecordPage(@Valid WithdrawnRecordPageReqVO pageReqVO) {
        PageResult<WithdrawnRecordDO> pageResult = withdrawnRecordService.getWithdrawnRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WithdrawnRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出待提现 Excel")
    @PreAuthorize("@ss.hasPermission('game:withdrawn-record:export')")
    @OperateLog(type = EXPORT)
    public void exportWithdrawnRecordExcel(@Valid WithdrawnRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WithdrawnRecordDO> list = withdrawnRecordService.getWithdrawnRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "待提现.xls", "数据", WithdrawnRecordRespVO.class,
                        BeanUtils.toBean(list, WithdrawnRecordRespVO.class));
    }

}